<template>
  <div>
    <el-dialog
      title="编辑订阅配置"
      :visible.sync="dialogVisible"
      width="510px"
      :before-close="closeAddDialog"
      :close-on-click-modal="false"
    >
      <el-form
        :model="editData"
        :rules="rules"
        ref="refAddForm"
        label-width="100px"
      >
        <el-form-item size="small" label="事件类型" prop="event">
          <el-select
            v-model="editData.event"
            disabled
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in eventList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item size="small" label="URL" prop="url">
          <el-input
            clearable
            v-model.trim="editData.url"
            placeholder="请输入"
            maxlength="100"
            style="width: 100%"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeAddDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="addSubmit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
import { reqSubscribeUpdate, reqOrgList } from "@api/api";
import { mapState } from "vuex";

export default {
  props: ["editData"],
  mixins: [],
  components: {},
  data() {
    return {
      dialogVisible: false,
      eventList: [
        {
          id: 1,
          name: "设备状态更新",
        },
        {
          id: 2,
          name: "告警事件",
        },
        {
          id: 3,
          name: "绑定事件",
        },
        {
          id: 4,
          name: "解绑事件",
        },
      ],
      // 表单验证规则
      rules: {
        url: [
          {
            required: true,
            message: "请输入URL",
            trigger: "blur",
          },
        ],
        targetId: [
          {
            required: true,
            message: "请选择组织机构",
            trigger: ["blur", "change"],
          },
        ],
        event: [
          {
            required: true,
            message: "请选择事件类型",
            trigger: ["blur", "change"],
          },
        ],
      },

      projectList: [], //组织机构数据
      sysInstanceIndex: 1,
      sysInstanceTotalPages: 10,
      loading: false,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  methods: {
    // async loadMore() {
    //     if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10)) return
    //     this.sysInstanceIndex++
    //     this.loading = true;
    //     // 获取列表数据的方法
    //     await this.OrgList()
    //     this.loading = false;
    // },
    // // 获取组织机构列表
    // async OrgList(flag) {
    //     var res = await reqOrgList(this.sysInstanceIndex, 10, {
    //         userId: this.user.userId,
    //         roleName: this.user.roleName,
    //         tenantId: this.user.tenantId,
    //     });
    //     if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
    //     if (flag) {
    //         this.editData.orgId = res.data.rows[0].id || "";
    //     }
    //     this.sysInstanceTotalPages = res.data.total
    //     this.projectList = [...this.projectList, ...res.data.rows]
    // },
    // 点击新增
    addSubmit() {
      this.$refs.refAddForm.validate(async (valid) => {
        if (!valid) return;
        const res = await reqSubscribeUpdate(this.editData);
        if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        this.$modal.notifySuccess("编辑订阅配置成功");
        this.closeAddDialog();
        this.$parent.getDataList();
      });
    },
    // 点击关闭弹框
    closeAddDialog() {
      this.$refs.refAddForm.resetFields();
      this.dialogVisible = false;
      // this.projectList= [] //组织机构数据
      // this.sysInstanceIndex= 1
      // this.sysInstanceTotalPages= 10
    },
  },
  mounted() {},
  deactivated() {
    if(this.$refs.refAddForm!=null){
      this.closeAddDialog();
    }
  },
};
</script>
  
<style lang="less" scoped>
@import url("../../../common.less");
</style>
  